package org.xshop.web;

import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xshop.bean.Cart;
import org.xshop.model.TCart;
import org.xshop.model.TShoppingCart;
import org.xshop.service.ITShoppingCartService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2017-04-23
 */
@Controller
@RequestMapping("/app/cart/")
@Api(tags = "ShoppingCart", value = "APP购物车接口", description = "APP-购物车接口")
public class TShoppingCartController extends AppBaseController<TShoppingCart, ITShoppingCartService> {
	@ApiOperation(value = "查询购物车", produces = MediaType.APPLICATION_JSON_VALUE, notes = "获取当前用户的购物车列表", response = TShoppingCart.class)
	@RequestMapping(value = "getShoppingcart.api", method = { RequestMethod.GET, RequestMethod.POST })
	public Object query(HttpServletRequest request, TCart cart) {
		Map<String, Object> param = WebUtil.getParameter(request);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.put("memberId", id);
		}
		if (DataUtil.isNotEmpty(param.get("goodsIds"))) {
			param.put("goodsIds", Arrays.asList(param.get("goodsIds").toString().split(",")));
		}
		Assert.notNull(param.get("memberId"), "用户ID不能为空.");
		Object result = service.queryAll(param);
		return setSuccessModelMap(result);
	}

	@PostMapping(value = "add.api")
	@ApiOperation(value = "添加购物车", notes = "将商品添加到购物车，需要商品、数量及规格", produces = MediaType.APPLICATION_JSON_VALUE, response = TShoppingCart.class)
	public Object update(HttpServletRequest request, Cart param) {
		param = WebUtil.getParameter(request, Cart.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setMemberId(id);
		}
		Assert.notNull(param.getMemberId(), "用户ID不能为空.");
		Assert.notNull(param.getSkuList(), "商品信息不能为空.");
		service.addCard(param);
		Map<String, Object> params = InstanceUtil.newHashMap("memberId", param.getMemberId());
		params.put("storeId", request.getParameter("storeId"));
		Object result = service.queryAll(params);
		return setSuccessModelMap(result);
	}

	@PostMapping(value = "delSkus.api")
	@ApiOperation(value = "删除商品", notes = "从购物车删除指定的商品", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object delete(HttpServletRequest request, Cart param) {
		param = WebUtil.getParameter(request, Cart.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setMemberId(id);
		}
		Assert.notNull(param.getMemberId(), "用户ID不能为空.");
		Assert.notNull(param.getSkuList(), "商品信息不能为空.");
		service.deleteCard(param);
		Map<String, Object> params = InstanceUtil.newHashMap("memberId", param.getMemberId());
		params.put("storeId", request.getParameter("storeId"));
		Object result = service.queryAll(params);
		return setSuccessModelMap(result);
	}

	@PostMapping(value = "modifySkuQty.api")
	@ApiOperation(value = "修改商品数量", notes = "修改购物车商品的数量", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object updateGoods(HttpServletRequest request, Cart param) {
		param = WebUtil.getParameter(request, Cart.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setMemberId(id);
		}
		Assert.notNull(param.getMemberId(), "用户ID不能为空.");
		Assert.notNull(param.getSkuList(), "商品信息不能为空.");
		service.updateCard(param);
		Map<String, Object> params = InstanceUtil.newHashMap("memberId", param.getMemberId());
		params.put("storeId", request.getParameter("storeId"));
		Object result = service.queryAll(params);
		return setSuccessModelMap(result);
	}

	@PostMapping(value = "delAll.api")
	@ApiOperation(value = "删除所有商品", notes = "从购物车删除所有的商品", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object deleteAll(HttpServletRequest request, Cart param) {
		param = WebUtil.getParameter(request, Cart.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setMemberId(id);
		}
		Assert.notNull(param.getMemberId(), "用户ID不能为空.");
		Map<String, Object> columnMap = InstanceUtil.newHashMap("member_id", param.getMemberId());
		service.deleteByMap(columnMap);
		return setSuccessModelMap();
	}

	@PostMapping(value = "updateSkus.api")
	@ApiOperation(value = "删除商品", notes = "从购物车删除指定的商品", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object updateShoppingCart(HttpServletRequest request, Cart param) {
		param = WebUtil.getParameter(request, Cart.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setMemberId(id);
		}
		Assert.notNull(param.getMemberId(), "用户ID不能为空.");
		service.updateShoppingCart(param);
		return setSuccessModelMap();
	}

}
